package com.moniu.xzts.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
public class ChatController {



    @Resource
    private  DeepSeekChatModel chatModel;

    @Autowired
    ChatMemory chatMemory;



    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "和我说一个笑话") String message) {
        return Map.of("generation", chatModel.call(message));
    }

    @Resource
    VectorStore vectorStore;
    @GetMapping(value = "/ai/generateStream", produces = "text/html;charset=UTF-8")
    public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "和我说一个笑话") String message) {
        var prompt = new Prompt(new UserMessage(message));
        ChatClient chatClient = ChatClient.builder(chatModel).defaultAdvisors(QuestionAnswerAdvisor.builder(vectorStore).build(),MessageChatMemoryAdvisor.builder(chatMemory).build()).build();
        Flux<String> content = chatClient.prompt(prompt)
                .advisors(new SimpleLoggerAdvisor())
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, 55))
                .tools(new DateTimeTools()).stream().content();
        return content;
    }
    public static record ActorsFilms(String name, List<String> movies) {
    }

    @GetMapping("/ai/test")
    public ActorsFilms test(){


        ActorsFilms actorsFilms = ChatClient.create(chatModel).prompt()
                .user(u -> u.text("请列举出周润发出演过那些电视剧或者电影"))
                .call()
                .entity(ActorsFilms.class);
        return actorsFilms;

    }

    class DateTimeTools {

        @Tool(description = "获取当前时间")
        String getCurrentDateTime() {
            return LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
        }
        @Tool(description = "获取电脑信息")
        String getavailableProcessors() {
            OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
//            System.out.println("系统架构：" + osBean.getArch());
//            System.out.println("可用处理器数量：" + osBean.getAvailableProcessors());
//            System.out.println("操作系统名称：" + osBean.getName());
//            System.out.println("操作系统版本：" + osBean.getVersion());
            return "系统架构：" + osBean.getArch()+
                    "可用处理器数量：" + osBean.getAvailableProcessors()+
                    "操作系统名称：" + osBean.getName()+
                    "操作系统版本：" + osBean.getVersion();

        }

    }
}
